package logic;

import com.jme.scene.Controller;

public abstract class DelayedController extends Controller {
	
	private static final long serialVersionUID = 1L;
	
	protected float updateDelay;
	protected float currentUpdateTime;
	
	public DelayedController() {}
	
	public DelayedController(float updateDelay) {
		this.updateDelay = updateDelay;
	}
	
	@Override
	public void update(float time) {
		extraUpdate(time);
		if(newUpdate(time)) delayedUpdate(time > updateDelay ? time : updateDelay);
	}
	
	@SuppressWarnings("unused")
	protected void extraUpdate(float time) {}
	
	protected abstract void delayedUpdate(float time);
	
	private boolean newUpdate(float time) {
		currentUpdateTime += time;
		if(updateDelay < currentUpdateTime) {
			currentUpdateTime -= updateDelay;
			return true;
		}
		return false;
	}
}